package com.otoe.dilidilivid.view.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.otoe.dilidilivid.R;
import com.otoe.dilidilivid.data.dao.VideoDao;
import com.otoe.dilidilivid.data.entity.VideoInfo;
import com.otoe.dilidilivid.adapter.VideoListAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link FragmentHome#newInstance} factory method to
 * create an instance of this fragment.
 */
public class FragmentHome extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";


    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private RecyclerView recyclerView;
    List<VideoInfo> list;
    VideoListAdapter adapter;

    public FragmentHome() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment home.
     */
    // TODO: Rename and change types and number of parameters
    public static FragmentHome newInstance(String param1, String param2) {
        FragmentHome fragment = new FragmentHome();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_home, container, false);
        @SuppressLint("HandlerLeak")
        final Handler handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                list=(ArrayList<VideoInfo>)msg.obj;
                List<VideoInfo> datas = list;
                int videoCover[]={R.drawable.video1,R.drawable.video2,R.drawable.video3,R.drawable.video4,R.drawable.video5, R.drawable.video6,R.drawable.video7,R.drawable.video8,R.drawable.video9,R.drawable.video10,R.drawable.video11, R.drawable.video12,R.drawable.video13,R.drawable.video14,R.drawable.video15,R.drawable.video16,R.drawable.video17, R.drawable.video18,R.drawable.video19,R.drawable.video20};

                int i=0;
                /*for(VideoInfo videoInfo:list){
                    videoInfo.setImgUrl(videoCover[i]);
                    i++;
                }*/
                adapter=new VideoListAdapter(FragmentHome.this,datas);
                recyclerView = (RecyclerView) v.findViewById(R.id.video_list);
                recyclerView.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false));
                recyclerView.setAdapter(adapter);
            }

        };
        new Thread(){
            @Override
            public void run(){
                VideoDao videoDao=new VideoDao();
                //videoDao.changeDuration();
                Message msg=Message.obtain();
                msg.obj=videoDao.selectAll();
                handler.sendMessage(msg);
            }
        }.start();

        return v;
    }

    @Override
    public void onResume() {
        super.onResume();
//        adapter.notifyItemChanged(0);
    }
}